class TuangouProsController < ApplicationController
  # GET /tuangou_pros
  # GET /tuangou_pros.xml
  layout "admin"
  before_filter :admin?,:except=>[:list,:show]
  before_filter :initialize_cart
  cache_sweeper :link_sweeper,:only=>[:create,:destroy,:update]
  def index
    @tuangou_pros = TuangouPro.paginate(:page=>params[:page],:per_page=>15)
    respond_to do |format|
      format.html 
      format.xml  { render :xml => @tuangou_pros }
    end
  end
  def list
    @tuangou_pros = TuangouPro.paginate(:page=>params[:page],:per_page=>20,:order=>"created_at desc",:select=>"a.*,b.name img",:joins=>" as a inner join pro_images b on a.product_id =b.product_id and img_type_id=1")
    @page_title="团购产品列表"
    @page_nav="><a href='/procates/all/products/list'>产品中心</a>>&nbsp;"
    @page_nav+="团购产品"
    respond_to do |format|
      format.html { render :layout=>"front_page"}
      format.xml  { render :xml => @tuangou_pros }
    end
  end
  # GET /tuangou_pros/1
  # GET /tuangou_pros/1.xml
  def show
    @tuangou_pro = TuangouPro.find(params[:id])
    @page_title=@tuangou_pro.title
    @product=@tuangou_pro.product
    @page_nav="><a href='/procates/all/products/list'>产品中心</a>>&nbsp;"
    @page_nav+="<a href='#{list_tuangou_pros_path}'>团购产品</a>>&nbsp;#{@page_title}"
    @tuangou_pro.increment!("click_count",1)

    respond_to do |format|
      format.html { render :layout=>"front_page"}
      format.xml  { render :xml => @tuangou_pro }
    end
  end

  # GET /tuangou_pros/new
  # GET /tuangou_pros/new.xml
  def new
    @tuangou_pro = TuangouPro.new

    respond_to do |format|
      format.html 
      format.js {render :layout=>"none"}
      format.xml  { render :xml => @tuangou_pro }
    end
  end

  # GET /tuangou_pros/1/edit
  def edit
    @tuangou_pro = TuangouPro.find(params[:id])
  end

  # POST /tuangou_pros
  # POST /tuangou_pros.xml
  def create
    @tuangou_pro = TuangouPro.new(params[:tuangou_pro])

    respond_to do |format|
      if @tuangou_pro.save
        flash[:notice] = 'TuangouPro was successfully created.'
        format.html { redirect_to(procate_products_path(0)) }
        format.xml  { render :xml => @tuangou_pro, :status => :created, :location => @tuangou_pro }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @tuangou_pro.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /tuangou_pros/1
  # PUT /tuangou_pros/1.xml
  def update
    @tuangou_pro = TuangouPro.find(params[:id])

    respond_to do |format|
      if @tuangou_pro.update_attributes(params[:tuangou_pro])
        flash[:notice] = 'TuangouPro was successfully updated.'
        format.html { redirect_to(tuangou_pros_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @tuangou_pro.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /tuangou_pros/1
  # DELETE /tuangou_pros/1.xml
  def destroy
    @tuangou_pro = TuangouPro.find(params[:id])
    @tuangou_pro.destroy

    respond_to do |format|
      format.html { redirect_to(tuangou_pros_url) }
      format.xml  { head :ok }
    end
  end
end
